﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;
using DevComponents.DotNetBar.Controls;
using Waiter.DataLayer;

namespace Waiter.Controler
{
    class HoursWorkCtrl
    {
        /// <summary>
        /// Load danh sách nhân viên để chấm công
        /// </summary>
        /// <param name="pWorkShiftCode">Code ca làm việc</param>
        /// <returns></returns>
        public List<VIEW_WorkShiftToGrid> fsGetStaffGridData(string pWorkShiftCode)
        {
            try
            {
                List<VIEW_WorkShiftToGrid> resultList = (from   wsm in CommonGlobalVariable.globalDataContext.WORK_SHIFTs
                                                         join   wsd in CommonGlobalVariable.globalDataContext.WORK_SHIFTS_Ds on wsm.CODE equals wsd.WORK_SHIFTS_CODE
                                                         join   stf in CommonGlobalVariable.globalDataContext.STAFFs on wsd.STAFF_CODE equals stf.CODE
                                                         where  wsm.CODE == pWorkShiftCode & stf.DEL_FLG == false
                                                         select new VIEW_WorkShiftToGrid
                                                         {
                                                             strStaffCode = stf.CODE,
                                                             strStaffName = stf.NAME,
                                                             bDelFlag = wsd.DEL_FLG
                                                         }).OrderByDescending(x=>x.bDelFlag).ThenBy(y=>y.strStaffName).ToList();

                return resultList;

            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "HoursWorkCtrl.fsGetStaffGridData",
                    CommonUtils.fsParamListToString(new object[1] { pWorkShiftCode }),
                    ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// Load danh sách nhân viên đã được chấm công
        /// </summary>
        /// <param name="pWorkShiftCode">Code ca làm việc</param>
        /// <param name="pDateKeeping">Ngày chấm công</param>
        /// <returns></returns>
        public List<HOURS_WORKED> fsGetHoursWorkedGridData(string pWorkShiftCode, DateTime pDateKeeping)
        {
            try
            {
               List<HOURS_WORKED> resultList = CommonGlobalVariable.globalDataContext.HOURS_WORKEDs.Where(x=>x.WORK_SHIFT_CODE == pWorkShiftCode & x.CREATE_DATE == pDateKeeping).OrderBy(x => x.STAFF.NAME).ToList();

                return resultList;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "HoursWorkCtrl.fsGetHoursWorkedGridData",
                    CommonUtils.fsParamListToString(new object[2] { pWorkShiftCode, pDateKeeping }),
                    ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// Get schema của table HOURS_WORKED để load vào grid
        /// </summary>
        /// <returns></returns>
        public List<HOURS_WORKED> fsGetNullListData()
        {
            List<HOURS_WORKED> resultList = CommonGlobalVariable.globalDataContext.HOURS_WORKEDs.Where(x => x.CODE == string.Empty).ToList();

            return resultList;
        }

        /// <summary>
        /// Load tên nhân viên vào ComboBox của grid
        /// </summary>
        /// <param name="cmb"></param>
        /// <history>Verion: 1.0.0.0 - TRAN_DANG_QUANG</history>
        /// <returns></returns>
        public ResultStatus fsLoadDataGridViewCombobox_Staff(DataGridViewComboBoxExColumn cmb)
        {
            List<STAFF> resultList = new List<STAFF>();

            try
            {
                resultList = CommonGlobalVariable.globalDataContext.STAFFs.Where(x =>x.DEL_FLG == false).OrderBy(x=>x.NAME).ToList();
                cmb.DataSource = resultList;

                cmb.ValueMember = "CODE";
                cmb.DisplayMember = "NAME";
                cmb.DataPropertyName = "STAFF_CODE";

                return ResultStatus.SUCCESS;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "HoursWorkCtrl.fsLoadDataGridViewCombobox_Staff",
                    "",
                    ex.ToString());
                cmb.DataSource = null;
            }

            return ResultStatus.ERROR;
        }

        /// <summary>
        /// Lưu data chấm công
        /// </summary>
        /// <param name="pListHoursWork">Danh sách chấm công (cùng một ca)</param>
        /// <returns></returns>
        public ResultStatus fsInsertDataHoursWork(List<HOURS_WORKED> pListHoursWork, string pCodeWorkShift, DateTime pCreateDate)
        {
            try
            {
                //Thêm mới thông tin chấm công
                long nCodeTmp = Convert.ToInt64(String.Format(CommonConstants.CODE_DATE, DateTime.Now));
                if (pListHoursWork != null && pListHoursWork.Count > 0)
                {
                    foreach (var item in pListHoursWork)
                    {
                        nCodeTmp++;
                        HOURS_WORKED myHourWork = new HOURS_WORKED();
                        myHourWork.CODE = nCodeTmp.ToString();
                        myHourWork.WORK_SHIFT_CODE = item.WORK_SHIFT_CODE;
                        myHourWork.CREATE_DATE = item.CREATE_DATE;
                        myHourWork.START_TIME = item.START_TIME;
                        myHourWork.END_TIME = item.END_TIME;
                        myHourWork.TIME_LENGHT = item.TIME_LENGHT;
                        myHourWork.STAFF_CODE = item.STAFF_CODE;
                        myHourWork.NOTE = item.NOTE;
                        myHourWork.OPID = item.OPID;
                        myHourWork.ADD_DATE = item.ADD_DATE;
                        myHourWork.UPD_DATE = item.UPD_DATE;

                        CommonGlobalVariable.globalDataContext.HOURS_WORKEDs.InsertOnSubmit(myHourWork);
                    }                    
                }

                //Xóa tất cả thông tin chấm công cũ
                List<HOURS_WORKED> lsToDelete = CommonGlobalVariable.globalDataContext.HOURS_WORKEDs.Where(x => x.WORK_SHIFT_CODE == pCodeWorkShift & x.CREATE_DATE == pCreateDate).ToList();

                for (int i = 0; i < lsToDelete.Count; i++)
                {
                    CommonGlobalVariable.globalDataContext.HOURS_WORKEDs.DeleteOnSubmit(lsToDelete[i]);
                }

                CommonGlobalVariable.globalDataContext.SubmitChanges();//Save data

                return ResultStatus.SUCCESS;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "HoursWorkCtrl.fsInsertDataHoursWork",
                    CommonUtils.fsParamListToString(pListHoursWork.ToArray())
                    + CommonUtils.fsParamListToString(new object[2] {pCodeWorkShift, pCreateDate }),
                    ex.ToString());
            }
            return ResultStatus.ERROR;
        }

        /// <summary>
        /// Load data vào grid của form tham chiếu
        /// </summary>
        /// <param name="pFromDate">Ngày bắt đầu</param>
        /// <param name="pToDate">Ngày kết thúc</param>
        /// <param name="strStaffCode">Code nhân viên (nếu không chọn thì input string.empty)</param>
        public List<VIEW_HoursWork_Grid> fsLoadRefGridData(DateTime pFromDate, DateTime pToDate, string strStaffCode)
        {                       
            List<DateTime> myDateCodeList = new List<DateTime>();
            List<HOURS_WORKED> myHWList = new List<HOURS_WORKED>();
            List<HOURS_WORKED> myHWSubList = new List<HOURS_WORKED>();
            VIEW_HoursWork_Grid tmpToAddList = new DataLayer.VIEW_HoursWork_Grid();
            List<VIEW_HoursWork_Grid> resultList = new List<DataLayer.VIEW_HoursWork_Grid>();            

            try
            {
                //Get data
                if(strStaffCode != string.Empty)
                {
                    myHWList = CommonGlobalVariable.globalDataContext.HOURS_WORKEDs.Where(x => x.STAFF_CODE == strStaffCode && x.CREATE_DATE >= pFromDate && x.CREATE_DATE <= pToDate).OrderBy(x=>x.CREATE_DATE).ToList();
                }

                //Chuyển data đã get thành list để đưa vào grid
                if (myHWList.Count > 0)
                {
                    myDateCodeList = myHWList.Select(x => x.CREATE_DATE).Distinct().ToList();

                    for (int i = 0; i < myDateCodeList.Count; i++)
                    {
                        myHWSubList = myHWList.Where(x => x.CREATE_DATE == Convert.ToDateTime(myDateCodeList[i])).OrderBy(x => x.CREATE_DATE).ToList();
                        tmpToAddList = new DataLayer.VIEW_HoursWork_Grid();
                        tmpToAddList.strTAFF_NAME = myHWSubList[0].STAFF.NAME;
                        tmpToAddList.dateWorkDay = myHWSubList[0].CREATE_DATE;

                        for (int j = 0; j < myHWSubList.Count; j++)
                        {
                            if (myHWSubList[j].WORK_SHIFT_CODE == "1")//Ca 1
                            {
                                tmpToAddList.bWorkShift1 = true;
                                tmpToAddList.strNote1 = myHWSubList[j].NOTE;
                            }
                            else if (myHWSubList[j].WORK_SHIFT_CODE == "2")
                            {
                                tmpToAddList.bWorkShift2 = true;
                                tmpToAddList.strNote2 = myHWSubList[j].NOTE;
                            }
                            else if (myHWSubList[j].WORK_SHIFT_CODE == "3")
                            {
                                tmpToAddList.bWorkShift3 = true;
                                tmpToAddList.strNote3 = myHWSubList[j].NOTE;
                            }
                            else
                            {
                                tmpToAddList.bWorkShift4 = true;
                                tmpToAddList.strNote4 = myHWSubList[j].NOTE;
                            }
                        }

                        resultList.Add(tmpToAddList);
                    }
                }   
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "HoursWorkCtrl.fsLoadRefGridData",
                    CommonUtils.fsParamListToString(new object[3] { pFromDate, pToDate, strStaffCode }),
                    ex.ToString());
            }

            return resultList;
        }
    }
}
